<%--
    Document   : Table
    Created on : 24-nov-2010, 14:00:43
    Author     : Gebruiker
--%>

<%@page import="com.sampullara.poker.Card"%>
<%@page import="model.Player"%>
<%@page import="model.GameManager"%>
<%@page import="model.Game"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Poker Table</title>
        <link href="poker.css" rel="stylesheet" type="text/css">
        <script type="text/javascript" language="javascript"><!--
            var xmlhttp;
            function getXMLHttpRequest() {
                if(window.XMLHttpRequest){
                    xmlhttp = new XMLHttpRequest();
                } else {
				xmlhttp = new ActiveObject("Microsoft.XMLHTTP");
				}
				return xmlhttp;

            }

            function autoTurn() {
                turn();
                buttonsDisabled();
                setInterval("turn()", 1000);
            }

            function turn() {
                xmlhttp = getXMLHttpRequest();
                xmlhttp.onreadystatechange=getResponse;
                xmlhttp.open("get", "TableController.do?update=now", true);
                xmlhttp.send(null);
            }


            function buttonsEnabled() {

            	var board = xmlhttp.responseXML.documentElement.getElementsByTagName("board");
				var highestBet = board[0].getElementsByTagName("highestBet");
				var players = xmlhttp.responseXML.documentElement.getElementsByTagName("players");
            	var p = players[0].getElementsByTagName("player");
            	var balance = p[0].getElementsByTagName("balance");
                
				if(highestBet[0].firstChild.nodeValue == 0) {
                	document.getElementById('check').disabled=false;
                	if(balance[0].firstChild.nodeValue > highestBet[0].firstChild.nodeValue) {
    	                document.getElementById('bet').disabled=false;
    	                document.getElementById('betamount').disabled=false;
    				}
				}
				else {
					if(balance[0].firstChild.nodeValue >= highestBet[0].firstChild.nodeValue) {
						document.getElementById('call').disabled=false;
	            	}
				}

                document.getElementById('fold').disabled=false;

            }

            function buttonsDisabled() {
                document.getElementById('check').disabled=true;
                document.getElementById('fold').disabled=true;
                document.getElementById('bet').disabled=true;
                document.getElementById('call').disabled=true;
                document.getElementById('raise').disabled=true;
                document.getElementById('betamount').disabled=true;
            }

            function submitFunction(i) {
                var action = "";
                if (i == 1) {
                	action = "check";
                }
                if (i == 2) {
                	action = "fold";
                }
                if (i == 3) {
                	action = "call";
                }
                if (i == 4) {
             	   action = "bet";
                }
                if (i == 5) {
                    action = "raise";
            }
                buttonsDisabled();
                xmlhttp = getXMLHttpRequest();
                xmlhttp.onreadystatechange=getResponse;
                if(action != "bet") {
	                var url = "TableController.do?turn=get&action=" + action;
	                xmlhttp.open("GET", url, true);
	                xmlhttp.send(null);
                }
                else {
					var url = "TableController.do?turn=get&action=" + action + "&amount=" + document.getElementById('betamount').value;
					xmlhttp.open("GET", url, true);
					xmlhttp.send(null);
                }
            }

          function getResponse(){
                if (xmlhttp.readyState==4 && xmlhttp.status==200) {

					var s = xmlhttp.responseXML.documentElement.getElementsByTagName("board");
					var state = s[0].getElementsByTagName("state");
					if(state[0].firstChild.nodeValue != "waiting") {

						document.getElementById('status').innerHTML='';
                    
	                	var x = xmlhttp.responseXML.documentElement.getElementsByTagName("turn");
	                	var turncheck = x[0].getElementsByTagName("turncheck");
	                	if(turncheck[0].firstChild.nodeValue == "true") {
	                    	buttonsEnabled();
	                	}
	                	else {
	                    	buttonsDisabled();
	                	}
	                	var playersTurn = x[0].getElementsByTagName("currentplayer");
	                	var currentplayer = playersTurn[0].firstChild.nodeValue;
	                	
						try {
		                	var hand = xmlhttp.responseXML.documentElement.getElementsByTagName("hand");
		                	var y = hand[0].getElementsByTagName("card");
		                	for(i=0;i<y.length;i++) {
								try {
									document.getElementById('p1card' + (i+1)).src=y[i].firstChild.nodeValue;
								} catch(er) { }
		                	}
						} catch(er) { }

						document.getElementById('betText1').innerHTML='Bet: ';
						document.getElementById('betText2').innerHTML='Bet: ';
						document.getElementById('betText3').innerHTML='Bet: ';
						document.getElementById('betText4').innerHTML='Bet: ';
						try {
		                	var players = xmlhttp.responseXML.documentElement.getElementsByTagName("players");
		                	var p = players[0].getElementsByTagName("player");
		                	for(c=0;c<p.length;c++) {
								try {
									var name = p[c].getElementsByTagName("name");
									var balance = p[c].getElementsByTagName("balance");
									var nameAndBalance = name[0].firstChild.nodeValue + ' - balance: ' + balance[0].firstChild.nodeValue;
									document.getElementById('player' + (c+1)).innerHTML=nameAndBalance;
									var bet = p[c].getElementsByTagName("bet");
									document.getElementById('p' + (c+1) + 'bet').innerHTML=bet[0].firstChild.nodeValue;

									if(name[0].firstChild.nodeValue == currentplayer) {
										document.getElementById('p' + (c+1) + 'turn').innerHTML='TURN';
									}
									else {
										document.getElementById('p' + (c+1) + 'turn').innerHTML='';
									}

									var dealer = s[0].getElementsByTagName("dealer");
									if(name[0].firstChild.nodeValue == dealer[0].firstChild.nodeValue) {
										document.getElementById('p' + (c+1) + 'dealer').innerHTML='(D)';
									}
									else {
										document.getElementById('p' + (c+1) + 'dealer').innerHTML='';
									}
									
									var folded = p[c].getElementsByTagName("folded");
									if(folded[0].firstChild.nodeValue == "true") {
										document.getElementById('p' + (c+1) + 'card1').src='images/blank.gif';
										document.getElementById('p' + (c+1) + 'card2').src='images/blank.gif';
									}
									else {
									}
								} catch(er) { }
		                	}
						} catch(er) { }

						if(state[0].firstChild.nodeValue == "waiting") { }
						else if(state[0].firstChild.nodeValue == "started") { }
						else if(state[0].firstChild.nodeValue == "preflop") {
							document.getElementById('card1').src='images/blank.gif';
							document.getElementById('card2').src='images/blank.gif';
							document.getElementById('card3').src='images/blank.gif';
							document.getElementById('card4').src='images/blank.gif';
							document.getElementById('card5').src='images/blank.gif';
							for(pl=1;pl<4;pl++) {
								document.getElementById('p' + (pl+1) + 'card1').src='images/back_red_casino.gif';
								document.getElementById('p' + (pl+1) + 'card2').src='images/back_red_casino.gif';
							}
							var pot = s[0].getElementsByTagName("pot");
							document.getElementById('potamount').innerHTML=pot[0].firstChild.nodeValue;
						}
						else {
							var pot = s[0].getElementsByTagName("pot");
							document.getElementById('potamount').innerHTML=pot[0].firstChild.nodeValue;
							
							var boardCards = s[0].getElementsByTagName("cards");
							var card = boardCards[0].getElementsByTagName("card");
							for(b=0;b<card.length;b++) {
								var cn = card[b].firstChild.nodeValue;
								document.getElementById('card' + (b+1)).src=cn;
							}

						}
						
					}
                }
            }

        </script>
    </head>
    <body onLoad="autoTurn()">
        <div class="wrapper">
			<div class="left1">
			</div>
			<div class="left2">
							<font color='red'><b id='p3dealer'></b></font><font color='green'><b id='p3turn'></b></font><br />
                            <b id="player3"></b><br />
							<b id="betText3"></b><b id="p3bet"></b>
							<br /><br />
                            <img src="images/back_red_casino.gif" id="p3card1" />
                            <img src="images/back_red_casino.gif" id="p3card2" />
			</div>
			<div class="left3">
			</div>
		</div>
		<div class="wrapper">
			 <div class="left1">
			 			 	 <font color='red'><b id='p2dealer'></b></font><font color='green'><b id='p2turn'></b></font><br />
                             <b id="player2"></b><br />
							 <b id="betText2"></b><b id="p2bet"></b>
							 <br /><br />
                             <img src="images/back_red_casino.gif" id="p2card1" />
                             <img src="images/back_red_casino.gif" id="p2card2" />
			 </div>
			 <div class="left2">
			 				<b id="status">Waiting for game to start...</b>
			 				<img src="images/blank.gif" id="card1" />
			 				<img src="images/blank.gif" id="card2" />
			 				<img src="images/blank.gif" id="card3" />
			 				<img src="images/blank.gif" id="card4" />
			 				<img src="images/blank.gif" id="card5" />
			 				Pot: <b id="potamount"></b>
                            <div id="board"></div>
			 </div>
			 <div class="left3">
			 				 <font color='red'><b id='p4dealer'></b></font><font color='green'><b id='p4turn'></b></font><br />
                             <b id="player4"></b><br />
							 <b id="betText4"></b><b id="p4bet"></b>
							 <br /><br />
                             <img src="images/back_red_casino.gif" id="p4card1" />
                             <img src="images/back_red_casino.gif" id="p4card2" />
			 </div>
		</div>
		<div class="wrapper">
			<div class="left1">
			</div>
			<div class="left2">
							<font color='red'><b id='p1dealer'></b></font><font color='green'><b id='p1turn'></b></font><br />
							<b id="player1"></b><br />
							<b id="betText1"></b><b id="p1bet"></b>
							<br /><br />
                            <img src="images/back_red_casino.gif" id="p1card1" />
                            <img src="images/back_red_casino.gif" id="p1card2" />
			</div>
			<div class="left3">
                            <form action="#" >
                            	<input type="text" id="betamount" name="betamount" value=""/><br />
                                <input type="button" id="check" name="check" value="Check" onClick="submitFunction(1)"/>
                                <input type="button" id="fold" name="fold" value="Fold" onClick="submitFunction(2)"/>
                                <input type="button" id="call" name="call" value="Call" onClick="submitFunction(3)"/>
                                <input type="button" id="bet" name="bet" value="Bet" onClick="submitFunction(4)"/>
                                <input type="button" id="raise" name="raise" value="Raise" onClick="submitFunction(5)"/>
                            </form>
			</div>
		 </div>
    </body>
</html>
